﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        int red1 = 0;
        int green1 = 1;
        int blue1 = 0;


        private void button1_Click(object sender, EventArgs e)
        {
            int digui1 = Convert.ToInt32(cDigui.Text);
            double leng1 = Convert.ToDouble(cleng.Text);
             th1 = Convert.ToDouble(th1zuo.Text) * Math.PI / 180;
             th2 = Convert.ToDouble(th2you.Text) * Math.PI / 180;
             per1 = Convert.ToDouble(per1zuo.Text);
             per2 = Convert.ToDouble(per2you.Text);
             red1 = Convert.ToInt32(red.Text);
             green1 = Convert.ToInt32(green.Text);
             blue1 = Convert.ToInt32(blue.Text);
            if (graphics == null) graphics = this.CreateGraphics();
            drawCayleyTree(digui1, 200, 400, leng1, -Math.PI / 2);
            
        }

       
        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            Pen myPen = new Pen(Color.FromArgb(red1, green1, blue1));
            graphics.DrawLine(
                myPen,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
